<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@1.6/lib/index.css">

		<!-- 引入组件 -->
		<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
		<script src="https://cdn.jsdelivr.net/npm/vant@1.6/lib/vant.min.js"></script>
		<script src="../js/ajax.js"></script>
		<script src="html5plus://ready"></script>
		<script src="../js/back.js"></script>
		<script src="../js/back1.js"></script>
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
	</head>
	<body>
		<div id="app">
			<header>
				<van-nav-bar style="background-color: red;" title="收货地址" right-text="删除" @click-left="onback1"  @click-right="delete1"
				 left-arrow></van-nav-bar>
			</header>
			<van-address-list v-model="addressId" :list="list" @add="onAdd" @edit="onEdit"></van-address-list>

		</div>
		<script>
			var vm = new Vue({
				el: "#app",
				data: {
					addressId: 0,
					list: [],
					obj:{
					name:"",
					tel:"",
					areaCode:"",
					addressDetail:"",
					isDefault:""
				   }

				},
				created:function(){
				var w = plus.webview.currentWebview();//得到当前页面对象
				console.log(w);
				console.log(this.obj);
				this.obj.name=w.name;
				this.obj.tel=w.phone;
				this.obj.areaCode=w.districtCode;
				this.obj.addressDetail=w.address;
				this.obj.isDefault=w.isdefault;
			},
				methods: {
					onback1: function() {
						plus.webview.close(plus.webview.currentWebview().id);
					},
					onAdd() {
						plus.webview.create("JEditress.html", "JEditress").show()
					
					},
					//编辑
					onEdit: function(addressInfo) {//保存修改
					console.log(addressInfo);
					var _this = this;
					var w = plus.webview.currentWebview();
					ajax({//编辑收货地址
						url: "http://dsapi.ysd3g.com/API/EditShippingAddress",
						dataType: "jsonp",
						data: {
							key: localStorage.getItem('key'), //登陆返回的Key
							addressId:w.addressId,//收货地址编号
							name:addressInfo.name,//收货人名称
							phone:addressInfo.tel,//收货人手机号码
							districtCode:addressInfo.areaCode,//收货人区县code
							address:addressInfo.addressDetail,//收货人详细地址
							isdefault:addressInfo.isdefault//是否为默认地址
						},
						success: function(res) {
							if(res.Error){
								_this.$toast.alert("出现错误，请稍后再试！")
							}else{
								plus.webview.close(plus.webview.currentWebview(),"slide-out-left");
								var shouhuo=plus.webview.getWebviewById("shouhuo");
								shouhuo.reload(true);
							}
						}
					});
					},
					//删除
					delete1() {
						_this = this,
							ajax({
								url: "http://dsapi.ysd3g.com/api/DeleteShippingAddress",
								data: {
									key: localStorage.getItem('key'),
									addressId: _this.list[_this.addressId].arrid
								},
								dataType: "jsonp",
								success: function(res) {
									if (!res.Error) {
										alert("删除成功");
										plus.webview.currentWebview().close();										
									} else {
										alert("失败")
									}
								}
							})
					}
					
				},
				mounted() {
					_this = this,
						ajax({
							url: "http://dsapi.ysd3g.com/API/MyShippingAddressList",
							data: {
								key: localStorage.getItem('key')
							},
							dataType: "jsonp",
							success: function(res) {
								// 							if(!res.Error){
								// 								var count=0;
								// 								for(var i=0;i<res.Data.length;i++){
								// 									var obj={};
								// 									obj.id=count++,
								// 									obj.name=res.Data[i].name
								// 									obj.tel=res.Data[i].tel
								// 									obj.address=res.Data[i].address
								// 									obj.arrid=res.Data[i].arrid
								// 									_this.list.push(obj)
								// 									console.log(arrid)
								// 								}
								if (!res.Error) {
									var count = 0;
									for (var i = 0; i < res.Data.length; i++) {
										var obj = {};
										obj.id = count++,
											obj.name = res.Data[i].name
										obj.tel = res.Data[i].tel
										obj.address = res.Data[i].address
										obj.arrid = res.Data[i].id
										_this.list.push(obj)
									}
								} else {
									alert("重新登录")
									plus.webview.create("Login.html", "Login").show();
								}
							}
						})
						
				}
			})
		</script>
	</body>
</html>
